package com.wenxixuanwu.fruitstore.controller;

import com.wenxixuanwu.fruitstore.service.exception.*;
import com.wenxixuanwu.fruitstore.util.JsonResult;
import org.apache.tomcat.util.http.fileupload.impl.FileUploadIOException;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/4/20
 */
public class BaseController {

    public static final int OK = 2000;

    @ExceptionHandler({ServiceException.class})
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> result = new JsonResult<>(e);
        if (e instanceof UserNotFundException){
            result.setState(4004);
            result.setMessage("未查询查询到用户！请联系管理员");
        } else if (e instanceof BannerNotFundException){
            result.setState(40041);
        }else if (e instanceof ProductNotFundException){
            result.setState(40042);
        }
        return  result;
    }
    //统一处理用户头像上传的文件抛出异常
    @ExceptionHandler(FileUploadException.class)
    public JsonResult<Void> FileException(Throwable e) {
        JsonResult<Void> result = new JsonResult<>(e);
        if (e instanceof FileEmptyException) {
            result.setState(6000);
            result.setMessage("上传的文件为空");
        } else if (e instanceof FileSizeException) {
            result.setState(6001);
            result.setMessage("上传的文件大小超过了限制");
        } else if (e instanceof FileTypeException) {
            result.setState(6003);
            result.setMessage("上传的文件类型出现了异常");
        } else if (e instanceof FileUploadIOException) {
            result.setState(6004);
            result.setMessage("上传的文件读写的时候出现了异常");
        }
        return result;
    }
}
